package com.eb.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 * @author Administrator
 *验证工具类
 */
public class DateUtil {
	public static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_DATE = "yyyy-MM-dd";
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getTime(){
		DateFormat format = new SimpleDateFormat(FORMAT_TIME);
		return format.format(new Date());
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getDate(){
		DateFormat format = new SimpleDateFormat(FORMAT_DATE);
		return format.format(new Date());
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getDateTime(String formatter){
		String str = "";
		try{
			DateFormat format = new SimpleDateFormat(formatter);
			str = format.format(new Date());
		}catch(Exception e){
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 日期转字符串
	 * @param date
	 * @param formatter
	 * @return
	 */
	public static String dateToStr(Date date,String formatter){
		String str = "";
		try{
			DateFormat format = new SimpleDateFormat(formatter);
			str = format.format(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 字符串转日期
	 * @param date
	 * @param formatter
	 * @return
	 */
	public static Date strToDate(String date,String formatter){
		Date dateTime = null;
		try{
			DateFormat format = new SimpleDateFormat(formatter);
			dateTime = format.parse(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dateTime;
	}
	/**
	 * long类型时间转Sring
	 * Unix时间戳 是10位
	 * Date.getTime() Long 13位
	 * @return
	 */
	public static Date longToDate(long longTime){
		Date dateTime = null;
		try{
			if((longTime+"").length() ==10)
				longTime = longTime * 1000;
			dateTime = new Date(longTime);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dateTime;
	}
	
	/**
	 * long类型时间转Sring
	 * Unix时间戳 是10位
	 * Date.getTime() Long 13位
	 * @return
	 */
	public static String longToStr(long longTime, String formatter){
		String dateTime = "";
		try{
			if((longTime+"").length() ==10)
				longTime = longTime * 1000;
			DateFormat format = new SimpleDateFormat(formatter);
			Date dt2 = new Date(longTime);
			dateTime = format.format(dt2);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dateTime;
	}
	
	public static void main(String[] args) {
		long l = new Date().getTime();
		System.out.println(longToStr(l, FORMAT_TIME));
	}
	
	
	
}
